var access = require('../access');

app.get('/events', function(request, response) {
	model.Event.find().sort('when', 1).run(function(e, events) {
		if (e) {
			response.render('error.jade', {
				title : 'Event Error',
				error : e,
				pretty : true
			});
		} else {
			response.render('Events/list.jade', {
				title : 'Home',
				events : events,
				pretty : true
			});
		}
	});
});

app.get('/events/new', access.admin, function(request, response) {
	response.render('Events/edit.jade', {
		title : 'Event',
		mode : 'add',
		pretty : true
	});
});

app.get('/events/:id', function(request, response) {
	model.Event.findOne({
		_id : request.params.id
	}, function(e, event) {
		if (e) {
			response.render('error.jade', {
				title : 'Event Error',
				error : e,
				pretty : true
			});
		} else if (event) {
			response.render('Events/view.jade', {
				title : 'Event',
				mode : 'view',
				event : event,
				pretty : true
			});
		} else {
			res.redirect('/');
		}
	});
});

app.get('/events/:id/edit', access.admin, function(request, response) {
	model.Event.findOne({
		_id : request.params.id
	}, function(e, event) {
		if (e) {
			response.render('error.jade', {
				title : 'Event Error',
				error : e,
				pretty : true
			});
		} else if (event) {
			response.render('Events/edit.jade', {
				title : 'Event',
				mode : 'update',
				event : event,
				pretty : true
			});
		} else {
			res.redirect('/');
		}
	});
});

app.get('/events/:id/remove', access.admin, function(request, response) {
	model.Event.remove({
		_id : request.params.id
	}, function(e, event) {
		if (e) {
			response.render('error.jade', {
				title : 'Event Error',
				error : e,
				pretty : true
			});
		} else {
			response.render('message.jade', {
				title : 'Event Removed',
				message : 'The event has been removed',
				next : '/events/',
				pretty : true
			});
		}
	});
});

app.post('/events', access.admin, function(request, response) {
	if (typeof (request.body.action) == 'string') {
		switch (request.body.action) {
		case 'add':
			var newEvent = new model.Event();
			newEvent.what = request.body.what;
			newEvent.where = request.body.where;
			newEvent.fbPlace = request.body.fbPlace;
			newEvent.when = request.body.when;
			newEvent.save(function(e) {
				if (e) {
					response.render('error.jade', {
						title : 'Event Error',
						error : e,
						pretty : true
					});
				} else {
					response.render('message.jade', {
						title : 'Event Created',
						message : 'The event has been created',
						next : '/events/' + newEvent._id,
						pretty : true
					});
				}
			});
			break;
		case 'update':
			model.Event.update({
				_id : request.body.id
			}, {
				what : request.body.what,
				where : request.body.where,
				fbPlace : request.body.fbPlace,
				when : request.body.when
			}, function(e) {
				if (e) {
					response.render('error.jade', {
						title : 'Update Error',
						error : e,
						pretty : true
					});
				} else {
					response.render('message.jade', {
						title : 'Event Updated',
						message : 'The event has been successfully updated',
						next : '/events/' + request.body.id,
						pretty : true
					});
				}
			});

			break;
		default:
			response.render('error.jade', {
				title : 'Event Error',
				error : 'The action defined for for the request was invalid!',
				pretty : true
			});
		}
	} else {
		response.render('error.jade', {
			title : 'Event Error',
			error : 'No action was defined for the request!',
			pretty : true
		});
	}
});
